#include "Comm.hpp"
#include "Reactor.hpp"
#include "Accepter.hpp"
#include "Util.hpp"
#include "Sock.hpp"
void Usage(std::string proc)
{
    std::cout << "Usage \r\t" << proc << " port" << std::endl;
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(1);
    }

    // 1. 创建套接字 listen_sock(3)
    int listen_sock = Sock::Socket();
    SetNonBlock(listen_sock);
    Sock::Bind(listen_sock, (uint16_t)atoi(argv[1]));
    Sock::Listen(listen_sock);

    // 2. 创建Reactor对象
    Reactor *R = new Reactor;
    // 构造epoll模型 _epfd(4)
    R->InitReactor();

    // 3. 给Reactor添加Event
    // 3.1 创建listen_sock对应的Event
    Event *pev = new Event;
    pev->_sock = listen_sock;
    pev->_R = R;
    pev->RegisterCallback(Accepter, nullptr, nullptr);
    // 3.2 将Event插入到Reactor中(ET模式)
    pev->_R->InsertEvent(pev, EPOLLIN | EPOLLET);

    // 4. Reactor派发就绪事件
    int timeout = 1000;
    for (;;)
    {
        R->Dispatcher(timeout);
    }

    return 0;
}